package com.kylin.module.ad.action;


import java.util.LinkedList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.criterion.Order;

import com.kylin.common.Constants;
import com.kylin.common.entity.MsgKey;
import com.kylin.common.paginate.DisplayPage;
import com.kylin.common.paginate.PageManager;
import com.kylin.hibernate.CriteriaInfo;
import com.kylin.hibernate.PropertyCondition;
import com.kylin.module.ad.business.ADBlogic;
import com.kylin.module.ad.forms.ADForm;
import com.kylin.module.ad.forms.ADTypeForm;
import com.kylin.module.ad.orm.AD;
import com.kylin.module.ad.orm.ADType;
import com.kylin.module.shop.orm.Company;
import com.kylin.struts.BaseDispatchAction;

public class ManageADAction extends BaseDispatchAction {
	protected static Logger log = Logger.getLogger(ManageADAction.class);
	
	/**
	 * 广告类型列表
	 * @param mapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward searchADType(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
		log.debug("广告类型列表 : searchId="+request.getParameter("searchId"));
		String forward = "listADType";
		int pageSize = 20;
		int pageNum = 1;
		String searchId = request.getParameter("searchId");
		CriteriaInfo cinfo = null;
		try {
			if (request.getParameter("page") != null) {
				pageNum = Integer.parseInt(request.getParameter("page"));
			}
			if (searchId == null) {
				cinfo = new CriteriaInfo(ADType.class, null, Order.asc("name"));
				addUserCacheCriteriaInfo(request, cinfo);
			}else{
				cinfo = getUserCacheCriteriaInfo(request, searchId);
			}
			DisplayPage page =  PageManager.getDisplayPage(pageSize, pageNum, cinfo);
			request.setAttribute(Constants.KEY_REQUEST_RESULT, page);
		}catch (Exception e) {
			doException( request, e, forward);
		}
		return mapping.findForward(forward);
	}

	/**
	 * 添加广告类型
	 * 
	 * @param mapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward addADType(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse response) {
		log.debug("开始添加广告类型");
		String forward = "addSucc";
		ADTypeForm form = (ADTypeForm) actionForm;
		try {
			ADBlogic blogic = new ADBlogic();
			blogic.addADType(form);
			// 返回成功的信息，需要在结果页面添加 <mytag:MyActionMsg />，也可以不使用此标签，直接在页面写成功信息
			request.setAttribute(Constants.MSGKEY, new MsgKey(
					"msg.addSpecial.succ", "添加广告类型成功"));
		} catch (Exception e) {
			doException(request, e, forward);
		}

		return mapping.findForward(forward);
	}


	/**
	 * 添加广告
	 * 
	 * @param mapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward addAD(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse response) {
		log.debug("开始添加广告");
		String forward = "addSucc";
		try {
			ADForm form = (ADForm) actionForm;
			log.debug("----form="+form);
			ADBlogic blogic = new ADBlogic();
			form.setAppPath(this.getServlet().getServletContext().getRealPath("/AD/"));
			blogic.addAD(form);
			// 返回成功的信息，需要在结果页面添加 <mytag:MyActionMsg />，也可以不使用此标签，直接在页面写成功信息
			request.setAttribute(Constants.MSGKEY, new MsgKey(
					"msg.addAD.succ", "添加广告成功"));
			log.debug("msg.addAD.succ : 添加广告成功");
		} catch (Exception e) {
			doException(request, e, forward);
		}

		return mapping.findForward(forward);
	}
	
	/**
	 * 广告列表
	 * @param mapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward searchAD(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
		log.debug("广告列表 : searchId="+request.getParameter("searchId"));
		String forward = "listAD";
		int pageSize = 20;
		int pageNum = 1;
		String searchId = request.getParameter("searchId");
		CriteriaInfo cinfo = null;
		try {
			if (request.getParameter("page") != null) {
				pageNum = Integer.parseInt(request.getParameter("page"));
			}
			if (searchId == null) {
				/*********构造查询条件***********/
				List conditions = new LinkedList(); //先创建个容器准备装条件
				String type = request.getParameter("adtype");
				if(type!= null && !type.equals("")){  //输入了查询条件：类别
					Long typeId = new Long(request.getParameter("adtype"));
					PropertyCondition cond = new PropertyCondition("adtype.id", typeId,PropertyCondition.OPERATOR_EQ );
					conditions.add(cond);
				}
				
				String name = request.getParameter("name");
				if(name!= null && !name.equals("")){  //输入了查询条件：名称
					PropertyCondition cond = new PropertyCondition("name", "%"+name+"%",PropertyCondition.OPERATOR_LIKE );
					conditions.add(cond);
				}
				String owner = request.getParameter("owner");
				if(owner!= null && !owner.equals("")){  //输入了查询条件：广告所属人
					PropertyCondition cond = new PropertyCondition("owner", "%"+ owner +"%",PropertyCondition.OPERATOR_LIKE);
					conditions.add(cond);
				}
				String language = request.getParameter("language");
				if(language!= null && !language.equals("")){  //输入了查询条件：广告所属人
					PropertyCondition cond = new PropertyCondition("language", language,PropertyCondition.OPERATOR_EQ);
					conditions.add(cond);
				}
				// 增加其他条件参照以上
				cinfo = new CriteriaInfo(AD.class, conditions);
				cinfo.setOrderby(Order.asc("adtype"));
				addUserCacheCriteriaInfo(request, cinfo);
			}else{
				cinfo = getUserCacheCriteriaInfo(request, searchId);
			}
			DisplayPage page =  PageManager.getDisplayPage(pageSize, pageNum, cinfo);
			request.setAttribute(Constants.KEY_REQUEST_RESULT, page);
		}catch (Exception e) {
			doException( request, e, forward);
		}
		return mapping.findForward(forward);
	}


	/**
	 * 显示编辑类别界面
	 * 
	 * @param mapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward editADType(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse response) {
		log.debug("显示编辑类别界面");
		String forward = "editADType";
		try {
			ADBlogic blogic = new ADBlogic();
			ADType obj = blogic.getADTypeById(new Long(request.getParameter("id")));
			// 返回成功的信息，需要在结果页面添加 <mytag:MyActionMsg />，也可以不使用此标签，直接在页面写成功信息
			request.setAttribute(Constants.KEY_REQUEST_RESULT, obj);;
		} catch (Exception e) {
			doException(request, e, forward);
		}
		return mapping.findForward(forward);
	}

	/**
	 * 更新广告类别
	 * 
	 * @param mapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward updateADType(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse response) {
		log.debug("更新广告类别");
		String forward = "editADType";
		ADTypeForm form = (ADTypeForm) actionForm;
		try {
			ADBlogic blogic = new ADBlogic();
			ADType obj = blogic.updateADType(form);
			// 返回成功的信息，需要在结果页面添加 <mytag:MyActionMsg />，也可以不使用此标签，直接在页面写成功信息
			request.setAttribute(Constants.MSGKEY, new MsgKey(
					"msg.updateADType.succ", "更新广告类别成功"));
			request.setAttribute(Constants.KEY_REQUEST_RESULT, obj);
		} catch (Exception e) {
			doException(request, e, forward);
		}

		return mapping.findForward(forward);
	}

	/**
	 * 显示编辑广告界面
	 * 
	 * @param mapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward editAD(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse response) {
		log.debug("显示编辑广告界面");
		String forward = "editAD";
		try {
			ADBlogic blogic = new ADBlogic();
			AD obj = blogic.getADById(new Long(request.getParameter("id")));
			// 返回成功的信息，需要在结果页面添加 <mytag:MyActionMsg />，也可以不使用此标签，直接在页面写成功信息
			request.setAttribute(Constants.KEY_REQUEST_RESULT, obj);
		} catch (Exception e) {
			doException(request, e, forward);
		}
		return mapping.findForward(forward);
	}
	/**
	 * 更新广告 
	 * 
	 * @param mapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward updateAD (ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse response) {
		log.debug("更新广告 ");
		String forward = "editAD";
		try {
			ADForm form = (ADForm) actionForm;
			log.debug("----form="+form);
			ADBlogic blogic = new ADBlogic();
			form.setAppPath(this.getServlet().getServletContext().getRealPath("/AD/"));
			AD obj= blogic.updateAD(form);
			// 返回成功的信息，需要在结果页面添加 <mytag:MyActionMsg />，也可以不使用此标签，直接在页面写成功信息
			request.setAttribute(Constants.MSGKEY, new MsgKey(
					"msg.updateAD.succ", "更新广告成功"));
			log.debug("msg.updateAD.succ : 更新广告成功");
			request.setAttribute(Constants.KEY_REQUEST_RESULT, obj);
		} catch (Exception e) {
			doException(request, e, forward);
		}
		return mapping.findForward(forward);
	}

}
